package com.lindroid.materialdesigndemo.tablayoutdemo;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.lindroid.materialdesigndemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author linyulong
 */
public class TabLayoutActivity extends AppCompatActivity {
    @Bind(R.id.fruit_image_view)
    ImageView fruitImageView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    private List<String> tabs = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        initData();
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            //显示标签上的文字
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs.get(position);
            }
        });

        tabLayout.setupWithViewPager(viewPager);
    }

    private void initData() {
        tabs.add("我的");
        tabs.add("发现");
        tabs.add("设置");

        fragments.add(new CommentFragment());
        fragments.add(new TabFragment(tabs.get(1)));
        fragments.add(new TabFragment(tabs.get(2)));
    }
}
